package com.zhijiaoyun.service.impl;

import com.zhijiaoyun.dao.ICommentDao;
import com.zhijiaoyun.model.Comment;
import com.zhijiaoyun.model.dto.PageData;
import com.zhijiaoyun.model.dto.PageList;
import com.zhijiaoyun.service.ICommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shisfish
 * @date 2017/12/26
 */
@Service(value = "commentService")
public class CommentServiceImpl implements ICommentService {

    @Resource
    ICommentDao commentDao;

    @Override
    public void insert(Comment comment) {
        commentDao.insert(comment);
    }

    @Override
    public Comment getById(long id) {
        return commentDao.selectByPrimaryKey(id);
    }

    @Override
    public void deleteById(long id) {
        Comment comment = getById(id);
        if (comment != null) {
            delete(comment);
        }
    }

    @Override
    public void delete(Comment comment) {
        commentDao.deleteByPrimaryKey(comment.getId());
    }

    @Override
    public PageList<Comment> pageByCourseId(int pageIndex, int pageSize, long courseId) {
        int count = commentDao.count(courseId);
        PageList<Comment> pageList = new PageList<>();
        if (count > 0) {
            PageData page = PageData.createPage(count, pageIndex, pageSize);
            List<Comment> list = commentDao.page(page.getBeginIndex(), page.getPageSize(), courseId);
            pageList = new PageList<>(list, page);
        }
        return pageList;
    }
}
